package cn.iocoder.yudao.module.squaredancing.controller.app.advertisement;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.squaredancing.controller.admin.advertisement.vo.AdvertisementPageReqVO;
import cn.iocoder.yudao.module.squaredancing.controller.admin.advertisement.vo.AdvertisementRespVO;
import cn.iocoder.yudao.module.squaredancing.controller.admin.advertisement.vo.AdvertisementSaveReqVO;
import cn.iocoder.yudao.module.squaredancing.dal.dataobject.advertisement.AdvertisementDO;
import cn.iocoder.yudao.module.squaredancing.service.advertisement.AdvertisementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 广告数据")
@RestController
@RequestMapping("/squaredancing/advertisement")
@Validated
public class AppAdvertisementController {

    @Resource
    private AdvertisementService advertisementService;

    @PermitAll
    @PostMapping("/create")
    @Operation(summary = "创建广告数据")
//    @PreAuthorize("@ss.hasPermission('squaredancing:advertisement:create')")
    public CommonResult<Long> createAdvertisement(@Valid @RequestBody AdvertisementSaveReqVO createReqVO) {
        return success(advertisementService.createAdvertisement(createReqVO));
    }

    @PermitAll
    @PutMapping("/update")
    @Operation(summary = "更新广告数据")
//    @PreAuthorize("@ss.hasPermission('squaredancing:advertisement:update')")
    public CommonResult<Boolean> updateAdvertisement(@Valid @RequestBody AdvertisementSaveReqVO updateReqVO) {
        advertisementService.updateAdvertisement(updateReqVO);
        return success(true);
    }

    @PermitAll
    @DeleteMapping("/delete")
    @Operation(summary = "删除广告数据")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('squaredancing:advertisement:delete')")
    public CommonResult<Boolean> deleteAdvertisement(@RequestParam("id") Long id) {
        advertisementService.deleteAdvertisement(id);
        return success(true);
    }

    @PermitAll
    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除广告数据")
//    @PreAuthorize("@ss.hasPermission('squaredancing:advertisement:delete')")
    public CommonResult<Boolean> deleteAdvertisementList(@RequestParam("ids") List<Long> ids) {
        advertisementService.deleteAdvertisementListByIds(ids);
        return success(true);
    }

    @PermitAll
    @GetMapping("/get")
    @Operation(summary = "获得广告数据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('squaredancing:advertisement:query')")
    public CommonResult<AdvertisementRespVO> getAdvertisement(@RequestParam("id") Long id) {
        AdvertisementDO advertisement = advertisementService.getAdvertisement(id);
        return success(BeanUtils.toBean(advertisement, AdvertisementRespVO.class));
    }

    @PermitAll
    @GetMapping("/page")
    @Operation(summary = "获得广告数据分页")
//    @PreAuthorize("@ss.hasPermission('squaredancing:advertisement:query')")
    public CommonResult<PageResult<AdvertisementRespVO>> getAdvertisementPage(@Valid AdvertisementPageReqVO pageReqVO) {
        PageResult<AdvertisementDO> pageResult = advertisementService.getAdvertisementPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AdvertisementRespVO.class));
    }

    @PermitAll
    @GetMapping("/export-excel")
    @Operation(summary = "导出广告数据 Excel")
//    @PreAuthorize("@ss.hasPermission('squaredancing:advertisement:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAdvertisementExcel(@Valid AdvertisementPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AdvertisementDO> list = advertisementService.getAdvertisementPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "广告数据.xls", "数据", AdvertisementRespVO.class,
                        BeanUtils.toBean(list, AdvertisementRespVO.class));
    }

}